package com.neusoft.nep.PublicSupervisor.controller;

import com.neusoft.nep.common.AlertUtils;
import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.entity.PublicSupervisorUser;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.URL;
import java.util.Map;
public class LoginController {
    @FXML
    private Button btnRegister;
    @FXML
    private PasswordField psPassword;
    @FXML
    private TextField txtPhoneNumber;
    @FXML
    private Button btnLogin;
    @FXML
    private Button btnForgetPassword;
    // 用于保存当前登录用户的姓名
    public static String currentUserName;
    //用于保存当前登录用户的生日
    public static String currentBirthday;
    //用于保存当前登录用户的性别
    public static String currentSex;
    //用于保存当前登录用户的手机号
    public static String currentPhoneNumber;
public void register() throws IOException {
    //获取Stage
    Stage stage = (Stage) btnForgetPassword.getScene().getWindow();
    //加载注册页面
    FXMLLoader fxmlLoader = new FXMLLoader();
    URL url = getClass().getResource("../View/Register.fxml");
    fxmlLoader.setLocation(url);
    Parent root = fxmlLoader.load();
    Scene scene = btnForgetPassword.getScene();
    scene.setRoot(root);
    stage.setScene(scene);
    stage.show();
}

public void forgetPassword() throws IOException {
    //获取Stage
    Stage stage = (Stage) btnRegister.getScene().getWindow();
    //加载注册页面
    FXMLLoader fxmlLoader = new FXMLLoader();
    URL url = getClass().getResource("../View/ChangePassword.fxml");
    fxmlLoader.setLocation(url);
    Parent root = fxmlLoader.load();
    Scene scene = btnRegister.getScene();
    scene.setRoot(root);
    stage.setScene(scene);
    stage.show();
}

public void login() throws IOException {
    Map<String, PublicSupervisorUser> userMap = FileUtils.readMapObject("PublicSupervisorUser.txt");
    String phoneNumber = txtPhoneNumber.getText();
    String password = psPassword.getText();
    if (userMap.containsKey(phoneNumber)) {
        PublicSupervisorUser u = userMap.get(phoneNumber);
        if (password.equals(u.getPassword())) {
            AlertUtils.showDialog("用户登录", "登陆成功");
            //保存用户名到feedback中
            currentUserName = u.getUsername();
            currentBirthday = u.getBirthday();
            currentSex = u.getSex();
            currentPhoneNumber = u.getPhoneNumber();
            //获取Stage
            Stage stage = (Stage) btnLogin.getScene().getWindow();
            //加载注册页面
            FXMLLoader fxmlLoader = new FXMLLoader();
            URL url = getClass().getResource("../View/Selectgrid.fxml");
            fxmlLoader.setLocation(url);
            Parent root = fxmlLoader.load();
            Scene scene = btnLogin.getScene();
            scene.setRoot(root);
            stage.setScene(scene);
            stage.show();
        } else {
            AlertUtils.showDialog("用户登录", "用户密码不正确");
        }
    } else {
        AlertUtils.showDialog("用户登录", "用户不存在");
    }

}
}